package com.ohos.carbon.slice.dialog;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.*;
import ohos.agp.components.element.ElementScatter;
import ohos.agp.window.dialog.CommonDialog;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

import java.util.Map;

import static com.ohos.carbon.slice.MainAbilitySlice.pf;

public class ProgressDialogActivity extends AbilitySlice {

    public Image back_button, image1, image2;
    public Text text_title;
    private AnimatorProperty animator;
    private Thread myFristThread;
    private int progress_fristThread = 1;
    private RoundProgressBar roundProgressBar;


    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_activity_progressdialog);
        final TextField titleText = (TextField) findComponentById(ResourceTable.Id_titleText2);
        final TextField progresText = (TextField) findComponentById(ResourceTable.Id_messageText);
        final TextField buttonText = (TextField) findComponentById(ResourceTable.Id_buttonText2);
        initView();

        Button button = (Button) findComponentById(ResourceTable.Id_showProgressDialog);
        button.setClickedListener(component -> {
            component = LayoutScatter.getInstance(getContext())
                    .parse(ResourceTable.Layout_progressdialog, null, true);
            Text text = (Text) component.findComponentById(ResourceTable.Id_progressText);
            text.setText(titleText.getText());
            Text text1 = (Text) component.findComponentById(ResourceTable.Id_progressContext);
            text1.setText(progresText.getText());
            Button button1 = (Button) component.findComponentById(ResourceTable.Id_progressButton);
            button1.setText(buttonText.getText());
            roundProgressBar = (RoundProgressBar) component.findComponentById(ResourceTable.Id_round_progress_bar);

            CommonDialog commonDialog = new CommonDialog(getContext());
            EventHandler eventHandler = new EventHandler(EventRunner.getMainEventRunner());
            commonDialog.setContentCustomComponent(component);
            commonDialog.show();
            eventHandler.postTask(commonDialog::destroy, 5000);
            button1.setClickedListener(component1 -> commonDialog.destroy());

            myFristThread = new FirstThread();
            myFristThread.start();
            animator = roundProgressBar.createAnimatorProperty();
            animator.setDuration(1200).rotate(360).setLoopedCount(AnimatorProperty.INFINITE).start();

        });

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("Progress dialog")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("Progress dialog")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("Progress dialog", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("Progress dialog", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("Progress dialog", 1);
            pf.flushSync();
        }
    }

    class FirstThread extends Thread {
        @Override
        public void run() {
            while (progress_fristThread < 89) {
                try {
                    roundProgressBar.setProgressValue(progress_fristThread++);
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            if (roundProgressBar.getProgress() == 90) {
                animator.alpha(0).setDuration(1000).start();
                animator.reset();
            }
        }
    }

    private void initView() {
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        image1 = (Image) findComponentById(ResourceTable.Id_image1);
        image2 = (Image) findComponentById(ResourceTable.Id_image2);
        image1.setVisibility(Component.VISIBLE);
        image2.setVisibility(Component.VISIBLE);
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        text_title.setText("Progress dialog");
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        back_button.setClickedListener(component -> onBackPressed());
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

}
